<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*自由岛软件  对接*/
class ZYDApi extends  BaseApi
{
	//进出门禁
	const validationPhone="/validationPhone";// 验证手机号码
	const bindUserVeinFinger="/bindUserVeinFinger";//绑定用户生物特征
	const signedMember="/signedMember"; //签到
	const turnoverNotice="/turnoverNotice"; //出场
	
	protected $url;//自由岛接口url
	protected $appid;//自由岛appid
	protected $deviceID;//自由岛设备Id
	protected $category;//自由岛品牌
	
	protected $shop_id;//中未智联店铺id
	
	 public function _initialize()
    {
    }
	/////////////////////////////////////////获取安科数据////////////////////////////////////////////////////////////////
	/**
	 * 方法前操作获取店铺信息
	 */
	function _before_method($shop_id){
		if(empty($shop_id)){
			$this->ajax_error('','',"店铺id 为空");
		}else{
			$this->shop_id=$shop_id;
		}
		$config=cache("ExtraConfig_$shop_id");
    	if(empty($config)){
			 $config=db("Shop")->where("id=".$this->shop_id)->value("extra_config");
			 if(empty($config)){
			 	$this->ajax_error('','',"请配置自由岛代码");
			}
			 $config=json_decode($config,TRUE);
			 cache("ExtraConfig_".$this->shop_id,$config);
    	}
		$this->url=$config['url'];
    	$this->appid=$config['appid'];
		$this->deviceID=$config['deviceID'];
		$this->category=$config['category'];
	}
	/**
	 * 获取合同号
	 * @param $card_no
	 * @return $data
	 * [
	 *	    {
	 *		        "ContractID": "06162ef0-7022-433e-80bd-7d9d44dc113c",
	 *		        "MemberID": "5c5313c8-aa5b-493a-8664-88e89b19a6ca",
	 *		        "MemberName": "ERCC",
	 *		        "Sex": "鐢�",
	 *		        "Mobile": "18523020102",
	 *		        "CardNo": "2238082314",
	 *		        "StartDate": "2017-10-19",
	 *		        "EndDate": "2018-10-28",
	 *		        "ContractStatus": 1,
	 *		        "MembershipType": 2,
	 *		        "MembershipTypeName": "骞存湀鍗�",
	 *		        "SurplusNum": "0",
	 *		        "StoredSumValue": "0.00"
	 *		    }
	 *		]
	 * http://smartgym.com/api/ZYDApi/GetContractInfo/card_no/123456_15202373874/shop_id/25
	 */
	function GetContractInfo($card_no,$shop_id,$mobile){
		$this->_before_method($shop_id);
		$uid=$this->GetUid($card_no, $mobile, $shop_id);//"1513833725";
		$sign_str="appid".$this->appid."category".$this->category."deviceID".$this->deviceID."numType"."1"."numValue".$mobile."uid".$uid;;
		$sign=array("sign:".md5($sign_str));
		//{"numValue":"13604419100","numType":"1","appid":"ZYd04911697874eA2a09D156A54894dc","deviceID":"zy001","uid":"1513833725","category":"gym"}
		$result=post_json($this->url.self::bindUserVeinFinger,['numValue'=>$mobile,"numType"=>"1",'appid'=>$this->appid,"deviceID"=>$this->deviceID,"uid"=>$uid,"category"=>$this->category],$sign);
		if($result['errcode']==0){
				return template_success("成功(自由岛)",array(
														  "ContractID"=>$uid,
														  "MemberID"=>$uid,
														  "StartDate"=>"1990-01-01",
														  "MemberName"=>$result['name'],
														  "EndDate"=>"2099-01-01",
														  "ContractStatus"=>1,
														  "Sex"=>$result['sex'],
														  "Mobile"=>$mobile,
														  "CardNo"=>$card_no,
														  "MembershipType"=>2,
														  "MembershipTypeName"=>'',
														  "SurplusNum"=>0,
														  "StoredSumValue"=>0,
														  )
													)
				;
		}else{
		   return  template_error($result['errmsg']);
		}
	}
	/**
	 * 入场操作
	 * @param $card_no
	 * http://smartgym.com/api/ZYDApi/CheckIn/card_no/123456/shop_id/25/member_id/1513833725
	 */
	function CheckIn($interface="",$brakemachine_no){
		//判断是否有权限
		if(empty($interface)){
			return template_error("暂时无权限!");
		}else{
			$door_auths=$interface['doors_auth'];
			if(empty($door_auths)){
				return template_error("暂无权限");
			}else{
				$brakemachine_no=intval($brakemachine_no);
				if(in_array($brakemachine_no,explode(",", $door_auths))){
					//有权限 continue
				}else{
					return template_error("暂无权限");	
				}
			}
		}
		$this->_before_method($interface['shop_id']);
		//第三方入场判断
		$json=post_json($this->url.self::turnoverNotice,['appid'=>$this->appid,"deviceID"=>$this->deviceID,"uid"=>$interface['member_id'],"type"=>0,"category"=>$this->category]);
		if($json['errcode']==0&&$json['userStatus']==0){
				return template_success("");
			}else{
				return template_error("拒绝入场或者会员过期(自由岛)-".$json["errmsg"]);
		}
	}
	/**
	 * 出场操作
	 * @param $card_no
	 * http://smartgym.com/api/ZYDApi/CheckIn/card_no/123456/shop_id/25/member_id/1513833725
	 */
	function ConfirmCheckOut($card_no,$shop_id,$member_id){
		$this->_before_method($shop_id);
		$json=post_json($this->url.self::turnoverNotice,['appid'=>$this->appid,"deviceID"=>$this->deviceID,"uid"=>$member_id,"type"=>1,"category"=>$this->category]);
		if($json['errcode']==0&&$json['userStatus']==0){
			return template_success("");		
		}else{
			return template_error($json['errmsg']);
		}
	}
	/**
	 * 获取uid
	 */
	function GetUid($card_no,$mobile,$shop_id){
		$uid=db("Interface")->where("card_no='$card_no' and mobile='$mobile' and shop_id=$shop_id " )->value("member_id");
		if(empty($uid)){
				return time();
		}else{
			return $uid;
		}
	}
}